#!/bin/bash
set -o pipefail;

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "


if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INSURANCE_FILE} ]
  then
    rm -f ${OUT_INSURANCE_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INSURANCE_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT

function usage()
{
  mllogerr "Usage: $PGNAME batch_recv_date_in_MMDDYY_fotmat";
}

if [ $# -lt 1 ]
then
  usage;
  exit 10;
else
  if [ "${2}" == "-s" ]
  then
    skip=1
  else
    skip=0
  fi
fi


# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${1}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"

# INSURANCE  FILE
IN_INSURANCE_FILE=${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_emdeon.gpg

if [ ! -d ${LOG_CTX_DIR} ]
then
  mkdir -p ${LOG_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : Unable to create log folder : ${LOG_CTX_DIR}";
    exit 10;
  fi
fi

if [ ! -r ${IN_INSURANCE_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : Incoming Emdeon Insurance file not found : ${IN_INSURANCE_FILE}";
  exit 10;
fi

tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : RUN PART-1 START"

${PGDIRNAME}/load_part_1.sh ${BATCH_RECV_DATE}
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : Load data part-1 failed"
  cleanup;
  exit 11;
fi

${PGDIRNAME}/proc_part_1.sh ${BATCH_RECV_DATE}
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : Process part-1 failed"
  cleanup;
  exit -1;
fi


# update batch status insurance2 done
${PGSQL} ${SQL_CTX_RUNT_DIR}/create_funcs_batch_status_insurance2.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : Batch status insurance2 done update failed"
  cleanup;
  exit -1;
fi


${PGDIRNAME}/back_part_1.sh ${BATCH_RECV_DATE}
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : Backup part-1 failed"
  cleanup;
  exit -1;
fi


tstamp="$(mltstamp ${tstart})"
mllogerr "[${LOG_CTX}] : END : [${tstamp}] : RUN PART-1 DONE"

exit 0;
